clear all
set more off
cd "/Users/yasenov/Dropbox (IPL)/RefugeeBan/data/"

***********
***********
***********

use "WRAPS/wraps_fips.dta"
keep if year >= 2010

merge 1:1 fips year using "crime/crime.dta", keep(1 3) keepusing(property violent cleared*)
drop if _merge == 1    
drop _merge

merge 1:1 fips year using "demographics/demographics.dta", keep(1 3)
drop if _merge == 1   
drop _merge

gen lproperty_crime = log(property_crime + 1)
gen rproperty_crime = (property_crime / pop_total) * 100000

gen lviolent_crime = log(violent_crime + 1)
gen rviolent_crime = (violent_crime / pop_total) * 100000

* treatment vars for year 2016
gen lrefugees2016 = log(refugees + 1)
gen rrefugees2016 = (refugee / pop_total) * 100

sort fips year
bysort fips: replace lrefugees2016 = lrefugees2016[7]
bysort fips: replace rrefugees2016 = rrefugees2016[7]

destring fips, replace
destring fips_state, replace

******************************
******************************

egen fips_ = group(fips)
tab year, gen(yrdum)

forvalues j = 1/9 {
	gen interaction`j' = rrefugees2016 * yrdum`j'
	gen interaction_log`j' = lrefugees2016 * yrdum`j'
	local k = `j' + 2009
	label var interaction`j' "`k'"
	label var interaction_log`j' "`k'"
	}
	
******************************
******************************
global controls
	
order *, alpha

drop interaction_log7 interaction7

preserve
reghdfe rproperty_crime interaction1-interaction9 $controls,  absorb(fips_ year) vce(cluster fips_state)
parmest, fast
keep if substr(parm,1,5) == "inter"
sort parm
gen year = 2009 + _n
replace year = year + 1 if year >= 2016
set obs 9
replace year = 2016 if year == .
replace est = 0 if year == 2016
twoway (scatter estimate year) ///
	(rcap min max year, lcolor(black)), ///
	legend(off) ///
	yline(0) ///
	xtitle("") ///
	xlabel(2010(1)2018) ///
	ytitle("{it:refugees{superscript:2016} } X Year") ///
	subtitle("Property Crime Rate") ///
	name(one, replace)
restore	


preserve
reghdfe rviolent_crime interaction1-interaction9 $controls, absorb(fips_ year) vce(cluster fips_state)
parmest, fast
keep if substr(parm,1,5) == "inter"
sort parm
gen year = 2009 + _n
replace year = year + 1 if year >= 2016
set obs 9
replace year = 2016 if year == .
replace est = 0 if year == 2016
twoway (scatter estimate year) ///
	(rcap min max year, lcolor(black)), ///
	legend(off) ///
	yline(0) ///
	xtitle("") ///
	xlabel(2010(1)2018) ///
	ytitle("{it:refugees{superscript:2016} } X Year") ///
	subtitle("Violent Crime Rate") ///
	name(two, replace)
restore	


preserve
reghdfe lproperty_crime interaction_log1-interaction_log9 $controls, absorb(fips_ year) vce(cluster fips_state)
parmest, fast
keep if substr(parm,1,5) == "inter"
sort parm
gen year = 2009 + _n
replace year = year + 1 if year >= 2016
set obs 9
replace year = 2016 if year == .
replace est = 0 if year == 2016
twoway (scatter estimate year) ///
	(rcap min max year, lcolor(black)), ///
	legend(off) ///
	yline(0) ///
	xtitle("") ///
	xlabel(2010(1)2018) ///
	ytitle("{it:log(refugees){superscript:2016} } X Year") ///
	subtitle("Log Property Crimes") ///
	name(thr, replace)
restore


preserve
reghdfe lviolent_crime interaction_log1-interaction_log9 $controls, absorb(fips_ year) vce(cluster fips_state)
parmest, fast
keep if substr(parm,1,5) == "inter"
sort parm
gen year = 2009 + _n
replace year = year + 1 if year >= 2016
set obs 9
replace year = 2016 if year == .
replace est = 0 if year == 2016
twoway (scatter estimate year) ///
	(rcap min max year, lcolor(black)), ///
	legend(off) ///
	yline(0) ///
	xtitle("") ///
	xlabel(2010(1)2018) ///
	ytitle("{it:log(refugees){superscript:2016} } X Year") ///
	subtitle("Log Violent Crimes") ///
	name(fou, replace)
restore

graph combine one two thr fou, imargin(vsmall)
graph export "../results/fig_event_study.pdf", replace
